function blogFunctions(){
		
        /*
         * Show / Hide Blog Editor
         */
		
		$("#blogEditor").corner();
		$("#blogText").corner();
		
        $("#blogEdit").bind("click", function(){
                $("#blogEditor").slideDown("fast");
                $("#blogEdit").hide("fast");
        });
        
        $("#cancelBlog").bind("click", function(){
                $("#blogEdit").show("fast");
                $("#blogEditor").slideUp("fast");
                $("#blogText").val("");
        });
        
        $("#blogText").bind("keyup", function(){
                var text = document.getElementById('blogText').value;
                var leftChars = 140 - text.length;
                $("#blcNumber").text(leftChars);
                
                if (text.length > 140){
                        $("#sendBlog").hide("slow");
                        $("#blogLeftChars").addClass("blogNoLeftChars");
                };
                if (text.length < 140){
                        $("#sendBlog").show("slow");
                        $("#blogLeftChars").removeClass("blogNoLeftChars");
                        $("#blogLeftChars").addClass("blogLeftChars");
                };
        });
        
        /*
         * Time Selector Event Listener
         */
        $("#portalTimeSelector").bind("change", function(){
                $("#blogroll").empty();
                getCurrentBlogEntries();
				$("#caseRoll").empty();
				getCurrentCases();
				$("#mediaRoll").empty();
				getCurrentMedia();
				
        });
        
        /*
         * Activate Time Selector
         */
        $("#timeSelector").bind("click", function(){
                $("#portalTimeDummy").hide();
                $("#portalTimeSelector").show();
        });
        
        /*
         * Blog: Event Listener
         */
		
		$("#blogEditButton").corner();
		
        $("#sendBlog").bind("click", function(){
                if ($("#blogText").val() == "" || $("#blogText").val() == " ") {
                        $("#emptyBlogMessage").dialog({
                                bgiframe: true,
                                modal: true,
                                buttons: {
                                        Ok: function(){
                                                $(this).dialog('close');
                                                $(this).dialog('destroy');
                                        }
                                },
                        });
                        return false;
                } else {
                        // get formular data
                        var inputData = $(":input");
                        var newBlogData = new Array();
                        for (i=0;i<inputData.length;i++){
                                if (inputData[i].parentNode.id == 'newBlogForm'){
                                                newBlogData.push(inputData[i].value);           
                                }
                        }
                        var userId = newBlogData[0];
                        var blogAuthor = newBlogData[1];
                        var blogText = newBlogData[2];
                        
                        
                        $.ajax({
                                type: "POST",
                                url: "../controller/writeBlog.php",
                                data: "userid=" + userId + "&author=" + blogAuthor + "&text=" + blogText,
                                
                                beforeSend: function(xml){
                                        // UI Response for sending a blogpost
                                },
                                
                                success: function(xml){
                                var status = $("status", xml).text();
                                         if (status == "success"){
                                                $("#blogstatusbarGood").fadeIn("slow");
                                                $("#blogEditor").fadeOut("slow");
                                                $("#blogEdit").fadeIn("slow");
                                                setTimeout("hideblogStatusBar()", 10000);
                                                
                                                // Clear the Blogroll and add the new items
                                                $("#blogroll").empty();
                                                getCurrentBlogEntries(); 
                                                $("#blogText").val("");
                                                
                                         }else{
                                                // show alert bar and keep editor open
                                                $("#blogstatusbarBad").fadeIn("slow");
                                                setTimeout("hideblogErrorBar()", 30000);
                                 };
                                 }
                        });                     
                };      
        });
        
	/*
	 * Destroy Dialog Object on Close
	 */
	$('#blogErrorMessage').bind('dialogclose', function(event, ui) {
        $(this).dialog('destroy');
	});
	
}

function getCurrentBlogEntries(){
        var lastDays = $("#portalTimeSelector option:selected").text();
        $.ajax({
                type: "GET",
                url: "../controller/readBlog.php",
                data: "lastdays="+lastDays,
                
                beforeSend: function(xml){
                        
                        // zeige Wartegrafik oder so...
                
                },
                success: function(xml){
                        $(xml).find('message').each(function(){
                        
                                var id = $(this).find('id').text();
                                var text = $(this).find('text').text();
                                var editor = $(this).find('editor').text();
                                var editorid = $(this).find('editorID').text();
                                var date = $(this).find('date').text();
                                
                                $('<div class="blogMessage" editor="editor_' + editorid + '" id="blog_' + id + '" blogid="' + id + '"></div>').html('<b>' + editor + '</b>' + ': ' + text + '<br/><i>' + date + '</i>').appendTo('#blogroll');
                                
                        });
                },
                complete: function(xml){
                        $(".blogMessage").corner();
                        // get hidden element containing user id
                		var loggedUser = $("#userCredentials").text();
                        
                        $("div.blogMessage[editor*="+loggedUser+"]").each(function(){
                                
                                // create delete button (hidden)
                                var tempID = $(this).attr("blogid");
                                var blogID = $(this).attr("id");
                                
                                //$("<button class='blogdeleteButton' blogid='"+tempID+"' />").appendTo(this);
                                $("<span>").html("<button class='blogdeleteButton' blogid='"+tempID+"' />").appendTo(this);
								$("<span>").html("<button class='tweetBlogEntry' blogid='"+tempID+"' />").appendTo(this);
                                
								/*
								 * tweet blog entry
								 */
								$(".tweetBlogEntry[blogid="+tempID+"]").bind("click", function(){
									
									var tweetText = $("#blog_"+tempID).text();
									window.open("http://twitter.com/home?status="+tweetText);
								
								});
								 
                               	/*
                               	 * delete blog entry
                               	 */ 
								// $("button[blogid="+tempID+"]").bind("click", function(){ 
                                $(".blogdeleteButton[blogid="+tempID+"]").bind("click", function(){
									$.ajax({
                                                
                                                type: "GET",
                                                url: "../controller/deleteBlog.php",
                                                data: "userid="+loggedUser+"&blogid="+tempID,
                                                
                                                beforeSend: function(xml){
                                					// wait...
                        						},
                                                
                                                success: function(xml){
                                                        var status = $("status", xml).text();
                                                        if (status == "success") {
                                                                $("#" + blogID).slideUp("slow");
                                                        } else {
                                                        	$("#blogErrorMessage").dialog({
							                                	bgiframe: true,
							                                	modal: true,
							                                	buttons: {
                                        							Ok: function(){
                                                						$(this).dialog('close');
                                                							$(this).dialog('destroy');
						                                        	}
						                                		},
						                        			});
                                                        }                               
                                                }
                                                
									});
                                        
								});
	
                                $(this).bind("mouseover", function(){
                                        $("button[blogid="+tempID+"]").fadeIn("fast");
                                });
                                
                                $(this).bind("mouseleave", function(){
                                        $("button[blogid="+tempID+"]").fadeOut("fast");
                                });
								
                        });
                }
        });
}

function tweetBlog(text){
	window.open("http://twitter.com/home?status="+text);	
}

function hideblogStatusBar(){
        $("#blogstatusbarGood").fadeOut("slow");
}
        
function hideblogErrorBar() {
        $("#blogstatusbarBad").fadeOut("slow");
}